
//
//  MainViewController.m
//  BabyAndBabyDriver
//
//  Created by 范琦 on 16/8/10.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "MainViewController.h"
#import "mainHeaderView.h"
#import "MainTableViewCell.h"
#import "MerchantServicesViewController.h"
#import "MoreInformationViewController.h"
#import "MainListModel.h"
#import "DetailInformationViewController.h"
@interface MainViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) mainHeaderView *mainHeaderView;
@property (nonatomic, strong) NSMutableArray *listArr;

@property (nonatomic, strong) NSString *head_img;
@property (nonatomic, strong) NSString *nickname;
@property (nonatomic, strong) NSNumber *month_jobs;
@property (nonatomic, strong) NSString *commission;

@property (nonatomic, strong) NSNumber *count;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatTableView];
    [self getData];
    [self getListData];
    _listArr = [NSMutableArray array];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)getData
{
    YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:@"User/info.json" paramDic:@{@"token":TOKEN} RegisterApiMethod:RequestMethodPost];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSLog(@"%@", request.responseBody);
        NSDictionary *dic = request.responseBody[@"result"];
        _head_img = dic[@"head_img"];
        _nickname = dic[@"nickname"];
        _month_jobs = dic[@"month_jobs"];
        _commission = dic[@"commission"];
        
        [_mainHeaderView.head_img sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@IMAGESMALL, _head_img]] placeholderImage:nil];
        _mainHeaderView.nickname.text = _nickname;
        NSString *str = [NSString stringWithFormat:@"本月佣金:%@",[_month_jobs stringValue]];
        _mainHeaderView.month_jobs.text = str;
        NSString *str1 = [NSString stringWithFormat:@"本月服务系数:%@", _commission];
        _mainHeaderView.commission.text = str1;
//        NSLog(@"%@",dic);
    } failure:^(YTKBaseRequest *request) {
        
    }];
}

- (void)getListData
{
    YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:@"Notice/lists.json" paramDic:@{@"token":TOKEN,@"firstRow":@0,@"listRows":@5} RegisterApiMethod:RequestMethodPost];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
//        NSLog(@"%@", request.responseBody);
        NSDictionary *dic = request.responseBody[@"result"];
        _count = dic[@"count"];
//        NSLog(@"%@", _count);
//        NSLog(@"%@",dic);
        NSArray *arr = dic[@"lists"];
        for (NSDictionary *dic1 in arr) {
            MainListModel *model = [[MainListModel alloc] init];
            model.Id = dic1[@"id"];
            model.data = dic1[@"date"];
            model.name = dic1[@"name"];
            model.context = dic1[@"context"];
            
            [_listArr addObject:model];
        }
        [_tableView reloadData];
    } failure:^(YTKBaseRequest *request) {
        
    }];
}

- (void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _mainHeaderView.frame.size.height, KSCREENWIDTH, KSCREENHEIGHT  - _mainHeaderView.bounds.size.height - 64) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"MainTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:_tableView];
    _tableView.sectionFooterHeight = 0;
    _tableView.sectionHeaderHeight = 0;
    
    self.mainHeaderView = [[[NSBundle mainBundle] loadNibNamed:@"mainHeaderView" owner:nil options:nil] lastObject];
    _mainHeaderView.frame = CGRectMake(0, 0, KSCREENWIDTH, 100 * KSCREENHEIGHT / 667);
    [[_mainHeaderView.MerchantServicesButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        MerchantServicesViewController *mer = [[MerchantServicesViewController alloc] init];
        [self.navigationController pushViewController:mer animated:YES];
    }];
    
    [self.view addSubview:_mainHeaderView];
    _tableView.tableHeaderView =_mainHeaderView;
    
}

#pragma mark - TableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _listArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [[[cell.moreInformationButton rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(id x) {
        MoreInformationViewController *more = [[MoreInformationViewController alloc] init];
        [self.navigationController pushViewController:more animated:YES];
    }];
    MainListModel *model = self.listArr[indexPath.section];
    cell.dateLabel.text = model.data;
    cell.nameLabel.text = model.name;
    cell.contextLable.text = model.context;
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailInformationViewController *detail = [[DetailInformationViewController alloc] init];
    MainListModel *model = _listArr[indexPath.row];
    detail.ID = model.Id;
    [self.navigationController pushViewController:detail animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
